package controllers.listeners.lexicon;

import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;

import models.OntologyModel;

import objects.Concept;
import objects.ConceptList;

import views.ViewLexicon;

/* Loads the Senses with respect to their Concept */
/* actionPerformed when you click on the concept combobox */
public class LoadConceptSenseItemListener implements ItemListener {
	
	private ViewLexicon view;
	
	public LoadConceptSenseItemListener() {
		view = null;
	}
	
	@Override
	public void itemStateChanged(ItemEvent arg0) {
		ArrayList<ConceptList> ontologyList = OntologyModel.getOntology();
		ConceptList conceptList = null;
		
		view.removeAllSenses();
		
		for (ConceptList currentConceptList : ontologyList) {
			/* Check if the selected category is found in the Ontology; therefore, not user-defined */
			if (view.getSelectedCategory().equalsIgnoreCase(currentConceptList.getLabel())) {
				conceptList = currentConceptList;
				break;
			}
		}
		
		/* User-defined */
		if (conceptList == null) {
			for (ConceptList currentConceptList : ontologyList) {
				for (Concept currentConcept : currentConceptList.getConceptList()) {
					if (view.getSelectedConcept() != null && view.getSelectedConcept().equals(currentConcept.getName())) {
						view.addSenseToChooser(currentConcept.getSense());
					}
				}
			}
		}
		/* Not user-defined */
		else {
			for (Concept currentConcept : conceptList.getConceptList()) {
				if (view.getSelectedConcept() != null && view.getSelectedConcept().equals(currentConcept.getName())) {
					view.addSenseToChooser(currentConcept.getSense());
				}
			}
		}
	}
	
	public void setView(ViewLexicon view) {
		this.view = view;
	}
}
